package com.study.modules.saas.repository.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.study.modules.saas.repository.model.MenuDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface MenuDao extends BaseMapper<MenuDO> {


    @Select("SELECT "
            + " m.id  "
            + "FROM "
            + " menu m "
            + " LEFT JOIN role_menu rm ON m.id = rm.menu_id  "
            + "WHERE "
            + " rm.role_id = #{roleId} "
            + " AND m.`status` = 'ENABLE' "
            + " AND m.deleted_at IS NULL")
    List<Integer> selectMenuIdByRoleId(@Param("roleId") Integer roleId);


    @Select("<script>SELECT "
            + " * "
            + "FROM "
            + " menu "
            + "WHERE "
            + " `status` = 'ENABLE' and id in "
            + " <foreach collection = 'menuIds' item='item' index = 'index' open='(' separator=','  close = ')'  >"
            + " #{item} </foreach>"
            + " AND deleted_at is null </script> ")
    List<MenuDO> selectMenuByIds(@Param("menuIds") List<Integer> menuIds);


    @Select("SELECT "
            + " m.*  "
            + "FROM "
            + " menu m "
            + " LEFT JOIN role_menu rm ON m.id = rm.menu_id  "
            + "WHERE "
            + " rm.role_id = #{roleId} "
            + " AND m.category = 'USER'"
            + " AND m.`status` = 'ENABLE'"
            + " AND m.deleted_at IS NULL ")
    List<MenuDO> selectMenuByRoleId(@Param("roledId") Integer roleId);

    @Select("SELECT "
            + " count(*) "
            + "FROM "
            + " menu m "
            + " LEFT JOIN role_menu rm ON m.id = rm.menu_id  "
            + "WHERE "
            + " m.id = #{menuId} "
            + " AND m.`status` = 'ENABLE'  "
            + " AND m.deleted_at IS NULL")
    int selectMenuByMenuId(@Param("menuId") Integer menuId);


}
